package com.prs.crm.action.project;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.project.ProjectStage;
import com.prs.crm.service.project.ProjectStageService;
import com.prs.crm.service.sys.LogService;

public class ProjectStageAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private ProjectStageService projectStageService;

	private PaginationSupport pagination;

	private ProjectStage stage;
	
	private String stageName;

	public String getStageName() {
		return stageName;
	}

	public void setStageName(String stageName) {
		this.stageName = stageName;
	}

	private Integer[] stageIds;
	private LogService logService;

	public void setProjectStageService(ProjectStageService projectStageService) {
		this.projectStageService = projectStageService;
	}

	public ProjectStageService getProjectStageService() {
		return projectStageService;
	}

	public ProjectStage getStage() {
		return stage;
	}

	public void setStage(ProjectStage stage) {
		this.stage = stage;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setStageIds(Integer[] stageIds) {
		this.stageIds = stageIds;
	}

	public Integer[] getStageIds() {
		return stageIds;
	}

	@Action("listStage")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				ProjectStage.class);
		this.setPagination(this.getProjectStageService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("editStage")
	public String edit() {
		return SUCCESS;
	}

	@Action(value = "saveStage", results = {
			@Result(name = "success", type = "redirect", location = "listStage"),
			@Result(name = "error", type = "chain", location = "editStage") })
	public String save() {
		if (stage.getName() == null || "".equals(stage.getName().trim())) {
			addActionError("阶段名称不能为空");
			return ERROR;
		}
		if (this.getLogService().getSearchName(ProjectStage.class,
				stage.getName())) {
			if(!(stage.getName().equals(stageName))){
			  this.addActionError("名称已经存在");
			  return ERROR;
			}
		}
		this.getProjectStageService().save(stage);
		return SUCCESS;
	}

	@Action(value = "removeStage", results = {
			@Result(name = "success", type = "redirect", location = "listStage"),
			@Result(name = "error", type = "chain", location = "listStage") })
	public String remove() {
		if (stageIds == null) {
			this.addActionError("请选择要删除的类型");
			return ERROR;
		}
		for (Integer id : stageIds) {
			ProjectStage stage = this.getProjectStageService().get(id);
			if (stage.getProjects().size() != 0) {
				this.addActionError("已有任务引用，不能删除");
				return ERROR;
			}
		}
		this.getProjectStageService().batchRemove(stageIds);
		return SUCCESS;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}

}
